//
//  UIBarButtonItem+Extension.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/25.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"
#import "NSString+Extension.h"

@implementation UIBarButtonItem (Extension)
+ (UIBarButtonItem *)sh_itemWithIcon:(NSString *)icon highIcon:(NSString *)highIcon target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
    button.frame = (CGRect){CGPointZero, button.currentBackgroundImage.size};
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

+ (UIBarButtonItem *)sh_itemWithIcon:(NSString *)icon highIcon:(NSString *)highIcon bageValue:(NSInteger)bageValue target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
    button.frame = (CGRect){CGPointZero, button.currentBackgroundImage.size};
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    if (bageValue> 0) {
        UIView *view = [[UIView alloc] init];
        CGFloat wh = 10;
        CGFloat radius = wh * 0.5;
        view.frame = CGRectMake(button.currentBackgroundImage.size.width - radius, -radius, wh, wh);
        view.cornerRadius = radius;
        view.tag = -1;
        view.backgroundColor = SHColor(254, 56, 36);
        [button addSubview:view];
    }
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

+ (UIBarButtonItem *)sh_itemWithTitle:(NSString *)title titleColor:(UIColor *)titleColor icon:(NSString *)icon highIcon:(NSString *)highIcon target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateHighlighted];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    [button setImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 0.0, 0, 0);
    CGFloat width = [title sizeWithFont:button.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, 44)].width + button.titleEdgeInsets.left + button.imageView.image.size.width;
    button.size = CGSizeMake(width, 22);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

+ (UIBarButtonItem *)sh_itemWithTitle:(NSString *)title icon:(NSString *)icon highIcon:(NSString *)highIcon target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    [button setImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 0.0, 0, 0);
    CGFloat width = [title sizeWithFont:button.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, 44)].width + button.titleEdgeInsets.left + button.imageView.image.size.width;
    button.size = CGSizeMake(width, 22);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

+ (UIBarButtonItem *)sh_itemWithTitle:(NSString *)title titleColor:(UIColor *)titleColor target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    CGFloat width = [title sizeWithFont:button.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, 44)].width + button.titleEdgeInsets.left + button.imageView.image.size.width;
    button.size = CGSizeMake(width, 22);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

+ (UIBarButtonItem *)sh_itemWithTitle:(NSString *)title target:(id)target action:(SEL)action{
    return [self sh_itemWithTitle:title titleColor:[UIColor blackColor] target:target action:action];
}

- (void)setBarButtonItemColor:(UIColor *)color {
    UIButton *btn = (UIButton *)self.customView;
    [btn setTitleColor:color forState:UIControlStateNormal];
}

- (void)setBarButtonItemTitle:(NSString *)title{
    UIButton *button = (UIButton *)self.customView;
    CGFloat width = [title sizeWithFont:button.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, 44)].width + button.titleEdgeInsets.left + button.imageView.image.size.width;
    button.size = CGSizeMake(width, 22);
    [button setTitle:title forState:UIControlStateNormal];
}

- (void)setBarButtonItemBageValue:(NSInteger)bageValue{
    UIButton *button = (UIButton *)self.customView;
    for (UIView *view in button.subviews) {
        if (view.tag == -1) {
            view.hidden = !bageValue;
            break;
        }
    }
    
}
@end
